package com.fingard.dsp.bank.directbank.ccb12;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CCB12DeContract extends CCB12Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try{
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem == null) {
                throw new Exception("未找到账号" + p_req.contractDetail.recAct + "对应的商户配置信息");
            }
            XmlTextWriter xmlWriter = new XmlTextWriter();
            if ("01".equals(p_req.contractDetail.step)) {
                txCode = "P5381916F";
                reqSeqID = p_req.contractDetail.askNumber;
                writeCommonHeader(xmlWriter, actItem);
                xmlWriter.writeStartElement("APP_ENTITY");
                xmlWriter.writeElementString("InsPolcy_No", p_req.contractDetail.insuPolicyNum);
                xmlWriter.writeElementString("Plchd_Nm", p_req.contractDetail.payName);
                xmlWriter.writeElementString("Plchd_Crdt_TpCd", convertCertTypeToBank(p_req.contractDetail.certType));
                xmlWriter.writeElementString("Plchd_Crdt_No", p_req.contractDetail.certNum);
                xmlWriter.writeElementString("Plchd_PyF_AccNo", p_req.contractDetail.payAct);
                xmlWriter.writeElementString("Mnplt_Tp_Ind", "3");
            } else if ("02".equals(p_req.contractDetail.step)) {
                txCode = "P5381916I";
                reqSeqID = p_req.reqHeader.transSeqID;
                writeCommonHeader(xmlWriter, actItem);
                xmlWriter.writeStartElement("APP_ENTITY");
                xmlWriter.writeElementString("InsPolcy_No", p_req.contractDetail.insuPolicyNum);
                xmlWriter.writeElementString("Plchd_Nm", p_req.contractDetail.payName);
                xmlWriter.writeElementString("Plchd_Crdt_TpCd", convertCertTypeToBank(p_req.contractDetail.certType));
                xmlWriter.writeElementString("Plchd_Crdt_No", p_req.contractDetail.certNum);
                xmlWriter.writeElementString("Plchd_PyF_AccNo", p_req.contractDetail.payAct);
                xmlWriter.writeElementString("SMS_Vld_CD", p_req.contractDetail.smsCode);
                xmlWriter.writeElementString("Orig_TxnSrlNo", p_req.contractDetail.token);
                xmlWriter.writeElementString("Cvr_ID", p_req.contractDetail.insureID, true);
            }
            xmlWriter.writeEndDocument();
            String tmpStrRet = sendToBank(xmlWriter.toXmlString());
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
            if (tmpBkReader != null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_DESC");
                String status = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_TX_STATUS");
                String respCode = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_CODE");
                if ("01".equals(p_req.contractDetail.step)) {
                    if (!"00".equals(status)) {
                        tmpRetDetail.respCode = "-1";
                    }
                    tmpRetDetail.token = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/COM_ENTITY/SvPt_Jrnl_No");
                    tmpRetDetail.step = "01";
                    tmpRetDetail.deSignState = ContractState.SUCCESS;
                } else {
                    if (!"00".equals(status)) {
                        tmpRetDetail.respCode = "-1";
                    }
                    if ("000000000000".equals(respCode)) {
                        tmpRetDetail.deSignState = ContractState.SUCCESS;
                    }else if ("YBLA0381213U".equals(respCode)) {
                        tmpRetDetail.deSignState = ContractState.SIGNING;
                    } else {
                        tmpRetDetail.deSignState = ContractState.FAIL;
                    }
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
